<?php
/**
 * @author Anh Nguyen
 * @copyright 2012
 */
 
defined('APP') or die('Denied access !');

class Default_Model_User extends Model
{
    public function userLogin($user, $pass)
    {
        $user = $this->sql_inject($user);
        $rs = $this->db->query("SELECT uAcc, uBanned, uStt FROM user WHERE uAcc='$user' AND uPwd='$pass'");
        if($rs->num_rows != 1) {
            return false;
        } else {
            $rt =  $rs->fetch_array(MYSQL_ASSOC);
            $this->free($rs);
            return $rt;
        }
    }
    
    public function userExists($user)
    {
        $user = $this->sql_inject($user);
        $rs = $this->db->query("SELECT uAcc FROM user WHERE uAcc = '$user'");
        if($rs->num_rows == 0) {
            $this->free($rs);
            return false;
        } else {
            $this->free($rs);
            return true;
        }
    }
    
    public function emailExists($email)
    {
        $email = $this->sql_inject($email);
        $rs = $this->db->query("SELECT uAcc FROM user WHERE uAcc = '$email'");
        if($rs->num_rows == 0) {
            $this->free($rs);
            return false;
        } else {
            $this->free($rs);
            return true;
        }
    }
    
    public function signUp($data, $birthday)
    {
        extract($data);
        $uAcc = $this->sql_inject($uAcc);
        $uPwd = Core::encrypt_pass($uPwd);
        $uEmail = $this->sql_inject($uEmail);
        $uName = $this->sql_inject($uName);
        $uGender =  $this->sql_inject($uGender);
        $uBirthday = $this->sql_inject($birthday);
        $uPlace = $this->sql_inject($uPlace);
        $uMobile = $this->sql_inject($uMobile);
        $uWork = $this->sql_inject($uWork);
        $sql = "INSERT INTO user (uAcc, uPwd, uEmail, uName, uGender, uBirthday, uPlace, uMobile, uWork, uStt, uBanned)
                            VALUES ('$uAcc','$uPwd', '$uEmail', '$uName', '$uGender', '$uBirthday', '$uPlace', '$uMobile', '$uWork', 0, 0)";
        if($this->db->query($sql))
            return true;
        return false;
    }
}
?>